% Generate 2D grids prior to plotting 2D feature space
%
% INPUTS
% x: numftrs x 2, 2D features.
% na, nb: number of grid on x-axis and y-axis (default values = 20, 20, respectively.
%
% OUTPUTS
% For example, xgrid is 400 by 2, xgrida is 20 by 20, xgridb is 20 by 20.
%
% 1. If numcols in x > 2, only the first two columns are used.
% xr = [mina, maxa; minb maxb]'
%   = [mina  minb
%      maxa  maxb]
%
% MAFPimentel, 24 May 2011

function [xgrid, xgrida, xgridb] = get_grid2D(x, na, nb)

if nargin < 2
    na = 20;
end

if nargin < 2
    nb = 20;
end

numcols = size(x,2);
% If numcols in x > 2, only the first two columns are used.
if numcols > 2
    fprintf('Number of features in x = %d, only first two feature are used.\n', numcols);
    x = x(:, 1:2); 
end

xr = minmax(x')'; % xr is 2 x 2 = [mina, maxa; minb maxb]'.
mina = xr(1,1);
maxa = xr(2,1);
minb = xr(1,2);
maxb = xr(2,2);
% OR 
mina = -3.95;
maxa = 3.95;
minb = -3.95;
maxb = 3.95;

% leave some margin around data.
% axis(xr(:) + 0.1*[-1 1 -1 1]'); 
mesha = linspace(mina - 0.05*abs(mina), maxa + 0.05*abs(maxa), na);
meshb = linspace(minb - 0.05*abs(minb), maxb + 0.05*abs(maxb), nb);
[xgrida,xgridb] = meshgrid(mesha, meshb); % xgrida and xgridb are 20 by 20.
xgrida1=reshape(xgrida, na*nb,1); % xgrida is 400 by 1.
xgridb1=reshape(xgridb, na*nb,1);
xgrid=[xgrida1 xgridb1]; % xgrid is 400 by 2
